﻿#include "ffmpegVideoPlayer.h"
#include "ui_ffmpegVideoPlayer.h"

#include <QObject>
#include <QDebug>
#include <QFileDialog>

class AudioOuputConfiguration audio_output_config;

ffmpegVideoPlayer::ffmpegVideoPlayer(QWidget *parent)
    : QWidget(parent),
      ui(new Ui::ffmpegVideoPlayer),
      player(this)
{
    ui->setupUi(this);

    connect(&(this->player),
            &videoPlayer::updatePlayTimeSignal,
            this,
            &ffmpegVideoPlayer::revUpdatePlayTimeToUISlot);
    connect(&(this->player),
            &videoPlayer::updatePlayImgSignal,
            this,
            &ffmpegVideoPlayer::revUpdatePlayImgToUISlot);
    connect(&(this->player),
            &videoPlayer::threadStartSuccessSignal,
            this,
            &ffmpegVideoPlayer::revThreadStartSuccessSlot);
    connect(&(this->player),
            &videoPlayer::finishedVideoPlaySignal,
            this,
            &ffmpegVideoPlayer::revFinishedVideoPlaySlot);
    connect(&(this->player),
            &videoPlayer::finishedVideoPlayThreadSignal,
            this,
            &ffmpegVideoPlayer::revFinishedVideoPlayThreadSlot);

    ui->hSldr_playTime->installEventFilter(this);
    this->player.playChangeShowQImageSize(ui->lb_imageDisplay->size()); //设置 显示窗口大小
}

ffmpegVideoPlayer::~ffmpegVideoPlayer()
{
    delete ui;
}

void ffmpegVideoPlayer::revThreadStartSuccessSlot(quint64 videoAllTime)
{
    this->videoAllTime = videoAllTime;
    QString allTime = QString::number(videoAllTime/3600) + \
                        ":" + \
                      QString::number(videoAllTime/60)   + \
                        ":" + \
                      QString::number(videoAllTime%60) ;
    this->ui->lb_sumTime->setText(allTime);
}

void ffmpegVideoPlayer::revUpdatePlayTimeToUISlot(quint64 playTime, quint64 videoAllTime)
{
    QString Time = QString::number(playTime / 3600) + \
                        ":" + \
                      QString::number(playTime / 60)   + \
                        ":" + \
                      QString::number(playTime % 60) ;
    this->ui->lb_currentTime->setText(Time);
    //更新进度条
    int value = playTime * 1.0 / videoAllTime * 100;
    //if(value > this->ui->hSldr_playTime->value())
    //{
        this->ui->hSldr_playTime->setValue(value);
    //}
}

void ffmpegVideoPlayer::revUpdatePlayImgToUISlot(QImage img)
{
    QPixmap my_pixmap;
    my_pixmap.convertFromImage(img);
    //设置 垂直居中
    this->ui->lb_imageDisplay->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    this->ui->lb_imageDisplay->setPixmap(my_pixmap);
}

void ffmpegVideoPlayer::revFinishedVideoPlaySlot()
{
    QString allTime = QString::number(this->videoAllTime/3600) + \
                        ":" + \
                      QString::number(this->videoAllTime/60)   + \
                        ":" + \
                      QString::number(this->videoAllTime%60) ;
    this->ui->lb_sumTime->setText(allTime);
    this->ui->lb_currentTime->setText(allTime);
    this->ui->hSldr_playTime->setValue(100);
}

void ffmpegVideoPlayer::revFinishedVideoPlayThreadSlot()
{
    qDebug() << tr("UI端:  等待player线程关闭!");

    qDebug() << tr("UI端:  player线程关闭成功!");
    this->ui->lb_currentTime->setText("00:00:00");
    this->ui->hSldr_playTime->setValue(0);
    //this->ui->tBtn_startPlay->clicked(true);
    this->ui->tBtn_startPlay->setText("    播放    ");
}


void ffmpegVideoPlayer::on_tBtn_loadVideoFile_clicked()
{
    player.playPause();
    this-> filename = QFileDialog::getOpenFileName(this,"选择播放的视频","D:/",tr("*.mp4 *.wmv *.*"));
    //qDebug()<<"播放的文件: "<< filename;
    if(this-> filename.isEmpty())
    {
        player.playContinue();
        return;
    }
    player.playStart(this->filename);
    this->ui->tBtn_startPlay->setText("    暂停    ");
}

void ffmpegVideoPlayer::on_tBtn_startPlay_clicked(bool checked)
{
    qDebug()<<"checked: "<<checked;

    if(!checked)//播放
    {
        this->ui->tBtn_startPlay->setText("    暂停    ");
        player. playContinue();
    }
    else//暂停
    {
        this->ui->tBtn_startPlay->setText("    播放    ");
        player. playPause();
    }
}

void ffmpegVideoPlayer::resizeEvent(QResizeEvent *event) //窗口大小变化事件
{
    int height=this->geometry().height() - ui->hSldr_playTime->height()*3-ui->tBtn_loadVideoFile->height();
    ui->lb_imageDisplay->setGeometry(0,0,this->width(),height);

    //获取显示视频的标签控件大小, 设置解码图片大小
    this->player.playChangeShowQImageSize(ui->lb_imageDisplay->size());
}


void ffmpegVideoPlayer::closeEvent(QCloseEvent *event) //窗口关闭
{
    this->player.playPause();
    int ret = QMessageBox::question(this, tr("视频播放器"),
    tr("是否需要退出程序?"),QMessageBox::Yes | QMessageBox::No);

    if(ret==QMessageBox::Yes)
    {
        this->player.playStop();
        if(this->player.isRunning())
        {
            this->player.quit();
            this->player.wait();
            qDebug() << tr("UI端:  player线程成功结束!");
        }

        event->accept();
    }
    else
    {
        this->player.playContinue();
        event->ignore();
    }
    /*
    其中accept就是让这个关闭事件通过并顺利关闭窗口，
    ignore就是将其忽略回到窗口本身。这里可千万得注意在每一种可能性下都对event进行处理，
    以免遗漏。
    */
}

bool ffmpegVideoPlayer::eventFilter(QObject *obj, QEvent *event)
{
    //解决QSlider点击不能到鼠标指定位置的问题
    if(obj==ui->hSldr_playTime)
    {
        if (event->type()==QEvent::MouseButtonPress)           //判断类型
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton)	//判断左键
            {
               int value = QStyle::sliderValueFromPosition(ui->hSldr_playTime->minimum(),
                                                           ui->hSldr_playTime->maximum(), mouseEvent->pos().x(),
                                                           ui->hSldr_playTime->width());
               ui->hSldr_playTime->setValue(value);
               this->player.playPause();
               this->player.playSeek(value);
               //qDebug() << tr("进度条点击值 val = %1").arg(value);
               //设置视频跳转
                //video_play_state.seek_pos=value; //秒单位
                //video_play_state.seek_flag=1; //跳转标志
            }
        }
    }
    return QObject::eventFilter(obj,event);
}
